﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Dominion.Core;
using Dominion.WPF.Commands;
using System.Windows.Input;
using System.Windows.Data;
using Dominion.WPF.Services;

namespace Dominion.WPF.ViewModels
{
    public class BankViewModel : ViewModelBase
    {

        public BankViewModel()
            : base()
        {
        }


        private ICollectionView treasuriesView;
        public ICollectionView TreasuriesAndVictoriesView
        {
            get
            {
                if (treasuriesView == null)
                    treasuriesView = CreateGroupCollectionViewFor(UserContext.CurrentGame.Board, "Name", x => (x is Treasure) || (x is Victory) || (x is Malediction));
                return treasuriesView;
            }
        }


        private ICollectionView kingdomsView;
        public ICollectionView KingdomsView
        {
            get
            {

                if (kingdomsView == null)
                    kingdomsView = CreateGroupCollectionViewFor(UserContext.CurrentGame.Board, "Name", x => (x is Kingdom));
                return kingdomsView;
            }
        }




        private CollectionViewGroup selectedTreasuriesAndVictories;
        public CollectionViewGroup SelectedTreasuriesAndVictories
        {
            get
            {
                return selectedTreasuriesAndVictories;
            }
            set
            {
                selectedKingdoms = null;
                selectedTreasuriesAndVictories = value;
            }
        }

        public CollectionViewGroup selectedKingdoms;
        public CollectionViewGroup SelectedKingdoms
        {   get
            {
                return selectedKingdoms;
            } 
             set
            {
                selectedTreasuriesAndVictories = null;
                selectedKingdoms = value;
            }
        }


        public  CollectionViewGroup SelectedCardGroup
        {
            get 
            {
                return SelectedTreasuriesAndVictories != null ? SelectedTreasuriesAndVictories : SelectedKingdoms;
            }
        }

        private RelayCommand buyCommand;
        public ICommand BuyCommand
        {
            get
            {
                if (buyCommand == null)
                    buyCommand = new RelayCommand(Buy, CanBuy);
                return buyCommand;
            }
        }


        private void Buy(object o)
        {
            UserContext.CurrentPlayer.BuyCard(SelectedCardGroup.Name.ToString());
        }

        private bool CanBuy(object card)
        {
            return SelectedCardGroup != null && UserContext.CurrentPlayer.CanBuyCard(SelectedCardGroup.Name.ToString());
        }

    }
}
